package cz.cuni.mff.ufal.volk;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;

import cz.cuni.mff.ufal.volk.command.AmbientCommand;
import cz.cuni.mff.ufal.volk.command.MessageCommand;
import cz.cuni.mff.ufal.volk.command.PingIntervalCommand;
import cz.cuni.mff.ufal.volk.command.RebootCommand;
import cz.cuni.mff.ufal.volk.nabaztag.ChoreographyPallette;
import cz.cuni.mff.ufal.volk.utils.Utils;

/**
 * <p>
 * Proxy to control the Nabaztag.
 * </p>
 * <p>
 * All public methods are guaranteed to be thread safe.
 * </p>
 *
 * @author Bartłomiej Etenkowski
 */
public class NabaztagProxy {

	private static final Logger log = Logger.getLogger(NabaztagProxy.class);

	public NabaztagProxy(String serial, String workingDirectory) {
		serial.getClass();
		workingDirectory.getClass();
		this.serial = serial;
		this.bunny = new NabaztagImpl(serial);
		this.workingDirectory = new File(workingDirectory);
		if (!this.workingDirectory.isDirectory())
			throw new IllegalArgumentException(
			    "The workingDirectory argument does not consist path of an existing directory");
	}

	private final NabaztagImpl bunny;
	private final String serial;
	private final File workingDirectory;

	final NabaztagImpl getBunny() {
		return bunny;
	}

	/**
	 * Returns the serial number of the Nabaztag.
	 *
	 * @return
	 */
	public final String getSerial() {
		return serial;
	}

	public void playSound(Speech s) {
		try {
			File temp = File.createTempFile("sound", ".wav", workingDirectory);
			log.trace("temporary file created: " + temp.getAbsolutePath());
			temp.deleteOnExit();
			Utils.writeAllBytes(temp, s.getData());
//			bunny.enqueueCommand(new MessageCommand(new PlayStreamMessage(
//					"/local/temp/" + temp.getName())));
			MessageCommand command = new MessageCommand();
			command.playSound("broadcast/local/temp/" + temp.getName());
			command.waitForPreviousCommand();
			bunny.enqueueCommand(command);
		} catch (IOException e) {
			log.error("An IOException occurred when trying to create temporary file",
			    e);
		}
	}

	public void playChoreography(byte[] bytes) {
		try {
			File temp = File.createTempFile("cho", ".chor", workingDirectory);
			log.trace("temporary file created: " + temp.getAbsolutePath());
			temp.deleteOnExit();
			Utils.writeAllBytes(temp, bytes);
			MessageCommand command = new MessageCommand();
			command.setChoreographyPallette(ChoreographyPallette.PAL_1);
			command.waitForPreviousCommand();
			command.playChoreography("broadcast/local/temp/" + temp.getName());
			//command.playChoreography("broadcast/local/temp/OrangeBlinkingWithEars.chor");
			command.waitForPreviousCommand();
			bunny.enqueueCommand(command);
		} catch (IOException e) {
			log.error("An IOException occurred when trying to create temporary file",
			    e);
		}
	}

	public void reboot() {
		bunny.enqueueCommand(new RebootCommand());
	}

	public void setPingInterval(int interval) {
		bunny.enqueueCommand(new PingIntervalCommand(interval));
	}

	public void ambient(AmbientCommand.Type type, byte left, byte right) {
		bunny.enqueueCommand(new AmbientCommand(type, left, right));
	}

	public void message(MessageCommand cmd) {
		bunny.enqueueCommand(cmd);
	}
}
